<?php

//Created by Joanna Tan 

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class results extends CI_Controller {

    /*
|--------------------------------------------------------------------------
| resultList (@author : Joanna Tan)
|--------------------------------------------------------------------------
|
| For Basic Search.
| This function will be use to check the search filter type, search 
| as a phrase option and returns the parsed Search XML results as 
| JSON and call the function to wrap the response in the
| requested callback. 
|
*/      
    public function resultList($conditionBasic) {
        $this->load->model('result_model');
        $quotes = "%22";
        $xmlOne = '';
        $searchUrl = $this->config->item('search_xml');
        $filtername = '';
        $optionChecked = '';

        list($filtertype, $keyword, $option) = explode("U5R", $conditionBasic);

        if ($filtertype == 'KeywordAnywhere') {
            $filtername = 'wrd';
        } else if ($filtertype == 'Author') {
            $filtername = 'wau';
        } else if ($filtertype == 'Title') {
            $filtername = 'wti';
        } else if ($filtertype == 'Subject') {
            $filtername = 'wsu';
        }

        if ($option == '1') {
            $optionChecked = 'on';
        } else if ($option == '0') {
            $optionChecked = 'off';
        }

        //replace word with encodings
        $keywordcheckSlash = $this->checkWord($keyword);

        if ($optionChecked == 'on') {
            $xmlOne = $searchUrl . $filtername . "=" . $quotes . $keywordcheckSlash . $quotes;
        } else if ($optionChecked == 'off') {
            $xmlOne = $searchUrl . $filtername . "=" . $keywordcheckSlash;
        }
        //replace encoding back to symbols
        $keywordpro = $this->replaceWord($keyword);
        $output = $this->result_model->praseFirstXml($xmlOne, $keywordpro);
        $this->retJsonP($output);
    }
	// Added by Sean
	public function ebooksResultList($conditionBasic) {
        $this->load->model('result_model');
        $quotes = "%22";
        $xmlOne = '';
        $searchUrl = $this->config->item('ebooks_xml');
        $filtername = '';
        $optionChecked = '';

        list($filtertype, $keyword, $option) = explode("U5R", $conditionBasic);

        if ($filtertype == 'KeywordAnywhere') {
            $filtername = 'wrd';
        } else if ($filtertype == 'Author') {
            $filtername = 'wau';
        } else if ($filtertype == 'Title') {
            $filtername = 'wti';
        } else if ($filtertype == 'Subject') {
            $filtername = 'wsu';
        }

        if ($option == '1') {
            $optionChecked = 'on';
        } else if ($option == '0') {
            $optionChecked = 'off';
        }

        //replace word with encodings
        $keywordcheckSlash = $this->checkWord($keyword);

        if ($optionChecked == 'on') {
            $xmlOne = $searchUrl . $filtername . "=" . $quotes . $keywordcheckSlash . $quotes . "+AND+(WNA%3Debook)";
        } else if ($optionChecked == 'off') {
            $xmlOne = $searchUrl . $filtername . "=" . $keywordcheckSlash . "+AND+(WNA%3Debook)";
        }
        //replace encoding back to symbols
        $keywordpro = $this->replaceWord($keyword);
        $output = $this->result_model->praseFirstXml($xmlOne, $keywordpro);
        $this->retJsonP($output);
    }
/*
|--------------------------------------------------------------------------
| checkWord (@author : Joanna Tan)
|--------------------------------------------------------------------------
|
| This function will be use to check specific search keyword and 
| replace it with URL-encoding
|
*/    
    public function checkWord($keyword) {
        $keyword = str_replace("slash", "%2F", $keyword);
        $keyword = str_replace("openbracket", "%28", $keyword);
        $keyword = str_replace("closebracket", "%29", $keyword);
        $keyword = str_replace("percent", "%25", $keyword);
        return $keyword;
    }
	
	// public function checkEbooksWord($keyword) {
        // $keyword = str_replace("slash", "%2F", $keyword);
        // $keyword = str_replace("openbracket", "%28", $keyword);
        // $keyword = str_replace("closebracket", "%29", $keyword);
        // $keyword = str_replace("percent", "%25", $keyword);
		// $keyword  += "+AND+(WNA%3Debook)" ;
        // return $keyword;
    // }
	
/*
|--------------------------------------------------------------------------
| replaceWord (@author : Joanna Tan)
|--------------------------------------------------------------------------
|
| This function will be use to check the keyword for URL-encoding
| and replace it with ASCII Character
|
*/    
    public function replaceWord($keyword) {
        $keywordpro = str_replace("%20", " ", $keyword);
        $keywordpro = str_replace("%22", " ", $keywordpro);
        $keywordpro = str_replace("%23", "#", $keywordpro);
        $keywordpro = str_replace("%2B", "+", $keywordpro);
        $keywordpro = str_replace("%2F", "/", $keywordpro);
        $keywordpro = str_replace("%40", "@", $keywordpro);
        $keywordpro = str_replace("%28", "(", $keywordpro);
        $keywordpro = str_replace("%29", ")", $keywordpro);
        $keywordpro = str_replace("%27", "'", $keywordpro);
        $keywordpro = str_replace("%21", "!", $keywordpro);
        $keywordpro = str_replace("%24", "$", $keywordpro);
        $keywordpro = str_replace("%25", "%", $keywordpro);
        $keywordpro = str_replace("%26", "&", $keywordpro);
        $keywordpro = str_replace("%2A", "*", $keywordpro);
        $keywordpro = str_replace("%3F", "?", $keywordpro);
        $keywordpro = str_replace("%5C", "\\", $keywordpro);
        $keywordpro = str_replace("%5B", "[", $keywordpro);
        $keywordpro = str_replace("%5D", "]", $keywordpro);
        $keywordpro = str_replace("%2C", ",", $keywordpro);
        $keywordpro = str_replace("%3B", ";", $keywordpro);
        return $keywordpro;
    }

/*
|--------------------------------------------------------------------------
| advanceSearchList (@author : Joanna Tan)
|--------------------------------------------------------------------------
| 
| For Advance Search.
| This function will be use to check the search filter type, search 
| as a phrase option and returns the parsed Search XML results as 
| JSON and call the function to wrap the response in the
| requested callback. 
|
*/    
    //Advance Search Checking of Condition
    public function advanceSearchList($conditionString) {
        $this->load->model('result_model');
        $quotes = "%22";
        $xmlAdvance = '';
        $searchUrl = $this->config->item('search_xml');

        $concludeOne = '';
        $concludeTwo = '';
        $concludeThree = '';
        $allConclude = '';
        $concludeKW = '';

        //split the conditions
        list($conOne, $conTwo, $conThree) = explode("R2E", $conditionString);
        //get first search condition
        list($filtertype1, $keyword1, $option1, $choice1) = explode("U5R", $conOne);

        $keyword1 = $this->checkWord($keyword1);
        if ($filtertype1 == 'Key') {
            $filtername1 = 'wrd';
        } else if ($filtertype1 == 'Author') {
            $filtername1 = 'wau';
        } else if ($filtertype1 == 'Title') {
            $filtername1 = 'wti';
        } else if ($filtertype1 == 'Subject') {
            $filtername1 = 'wsu';
        }

        if ($option1 == '1') {
            $optionChecked1 = 'on';
        } else if ($option1 == '0') {
            $optionChecked1 = 'off';
        }

        if ($optionChecked1 == 'on') {
            $concludeOne = '(' . $filtername1 . "=" . $quotes . $keyword1 . $quotes . ')';
        } else if ($optionChecked1 == 'off') {
            $concludeOne = '(' . $filtername1 . '=' . $keyword1 . ')';
        }
        $allConclude = $concludeOne;
        $keyword1process = $this->replaceWord($keyword1);
        $concludeKW = $keyword1process;

        //condition 2
        list($filtertype2, $keyword2, $option2, $choice2) = explode("U5R", $conTwo);
        $keyword2 = $this->checkWord($keyword2);
        if ($keyword2 == '') {
            //do nothing
        } else {
            if ($filtertype2 == 'Key') {
                $filtername2 = 'wrd';
            } else if ($filtertype2 == 'Author') {
                $filtername2 = 'wau';
            } else if ($filtertype2 == 'Title') {
                $filtername2 = 'wti';
            } else if ($filtertype2 == 'Subject') {
                $filtername2 = 'wsu';
            }

            if ($option2 == '1') {
                $optionChecked2 = 'on';
            } elseif ($option2 == '0') {
                $optionChecked2 = 'off';
            }

            if ($optionChecked2 == 'on') {
                $concludeTwo = '(' . $filtername2 . "=" . $quotes . $keyword2 . $quotes . ')';
            } else if ($optionChecked2 == 'off') {
                $concludeTwo = '(' . $filtername2 . '=' . $keyword2 . ')';
            }
            $allConclude = $allConclude . '+' . $choice2 . '+' . $concludeTwo;
            $keyword2process = $this->replaceWord($keyword2);
            $concludeKW = $concludeKW . ', ' . $keyword2process;
        }

        //Condition 3
        list($filtertype3, $keyword3, $option3, $choice3) = explode("U5R", $conThree);
        $keyword3 = $this->checkWord($keyword3);
        if ($keyword3 == '') {
            //do nothing
        } else {
            if ($filtertype3 == 'Key') {
                $filtername3 = 'wrd';
            } else if ($filtertype3 == 'Author') {
                $filtername3 = 'wau';
            } else if ($filtertype3 == 'Title') {
                $filtername3 = 'wti';
            } else if ($filtertype3 == 'Subject') {
                $filtername3 = 'wsu';
            }

            if ($option3 == '1') {
                $optionChecked3 = 'on';
            } elseif ($option3 == '0') {
                $optionChecked3 = 'off';
            }

            if ($optionChecked3 == 'on') {
                $concludeThree = '(' . $filtername3 . "=" . $quotes . $keyword3 . $quotes . ')';
            } else if ($optionChecked3 == 'off') {
                $concludeThree = '(' . $filtername3 . '=' . $keyword3 . ')';
            }
            $allConclude = $allConclude . '+' . $choice3 . '+' . $concludeThree;
            $keyword3process = $this->replaceWord($keyword3);
            $concludeKW = $concludeKW . ', ' . $keyword3process;
        }

        $xmlAdvance = $searchUrl . '(' . $allConclude . ')';

        $output = $this->result_model->praseFirstXml($xmlAdvance, $concludeKW);
        $this->retJsonP($output);
    }
	// Added by Sean
	public function examSearchList($conditionString) {
        $this->load->model('result_model');
        $quotes = "%22";
        $xmlAdvance = '';
        $searchUrl = $this->config->item('exam_xml');

        $concludeOne = '';
        $concludeTwo = '';
        $concludeThree = '';
        $allConclude = '';
        $concludeKW = '';

        //split the conditions
        list($conOne, $conTwo, $conThree) = explode("R2E", $conditionString);
        //get first search condition
        list($filtertype1, $keyword1, $option1, $choice1) = explode("U5R", $conOne);

        $keyword1 = $this->checkWord($keyword1);
        if ($filtertype1 == 'Title') {
            $filtername1 = 'wti';
        } else if ($filtertype1 == 'School') {
            $filtername1 = 'wau';
        } else if ($filtertype1 == 'PartTimeDip') {
            $filtername1 = 'wno';
        } else if ($filtertype1 == 'Exam') {
            $filtername1 = 'wyr';
		} else if ($filtertype1 == 'Level') {
            $filtername1 = 'wno';
        }

        if ($option1 == '1') {
            $optionChecked1 = 'on';
        } else if ($option1 == '0') {
            $optionChecked1 = 'off';
        }

        if ($optionChecked1 == 'on') {
            $concludeOne = '(' . $filtername1 . "=" . $quotes . $keyword1 . $quotes . ')';
        } else if ($optionChecked1 == 'off') {
            $concludeOne = '(' . $filtername1 . '=' . $keyword1 . ')';
        }
        $allConclude = $concludeOne;
        $keyword1process = $this->replaceWord($keyword1);
        $concludeKW = $keyword1process;

        //condition 2
        list($filtertype2, $keyword2, $option2, $choice2) = explode("U5R", $conTwo);
        $keyword2 = $this->checkWord($keyword2);
        if ($keyword2 == '') {
            //do nothing
        } else {
            if ($filtertype2 == 'Title') {
				$filtername2 = 'wti';
			} else if ($filtertype2 == 'School') {
				$filtername2 = 'wau';
			} else if ($filtertype2 == 'PartTimeDip') {
				$filtername2 = 'wno';
			} else if ($filtertype2 == 'Exam') {
				$filtername2 = 'wyr';
			} else if ($filtertype2 == 'Level') {
				$filtername2 = 'wno';
			}

            if ($option2 == '1') {
                $optionChecked2 = 'on';
            } elseif ($option2 == '0') {
                $optionChecked2 = 'off';
            }

            if ($optionChecked2 == 'on') {
                $concludeTwo = '(' . $filtername2 . "=" . $quotes . $keyword2 . $quotes . ')';
            } else if ($optionChecked2 == 'off') {
                $concludeTwo = '(' . $filtername2 . '=' . $keyword2 . ')';
            }
            $allConclude = $allConclude . '+' . $choice2 . '+' . $concludeTwo;
            $keyword2process = $this->replaceWord($keyword2);
            $concludeKW = $concludeKW . ', ' . $keyword2process;
        }

        //Condition 3
        list($filtertype3, $keyword3, $option3, $choice3) = explode("U5R", $conThree);
        $keyword3 = $this->checkWord($keyword3);
        if ($keyword3 == '') {
            //do nothing
        } else {
            if ($filtertype3 == 'Title') {
				$filtername3 = 'wti';
			} else if ($filtertype3 == 'School') {
				$filtername3 = 'wau';
			} else if ($filtertype3 == 'PartTimeDip') {
				$filtername3 = 'wno';
			} else if ($filtertype3 == 'Exam') {
				$filtername3 = 'wyr';
			} else if ($filtertype3 == 'Level') {
				$filtername3 = 'wno';
			}

            if ($option3 == '1') {
                $optionChecked3 = 'on';
            } elseif ($option3 == '0') {
                $optionChecked3 = 'off';
            }

            if ($optionChecked3 == 'on') {
                $concludeThree = '(' . $filtername3 . "=" . $quotes . $keyword3 . $quotes . ')';
            } else if ($optionChecked3 == 'off') {
                $concludeThree = '(' . $filtername3 . '=' . $keyword3 . ')';
            }
            $allConclude = $allConclude . '+' . $choice3 . '+' . $concludeThree;
            $keyword3process = $this->replaceWord($keyword3);
            $concludeKW = $concludeKW . ', ' . $keyword3process;
        }

        $xmlAdvance = $searchUrl . '(' . $allConclude . ')';

        $output = $this->result_model->praseFirstXml($xmlAdvance, $concludeKW);
        $this->retJsonP($output);
    }

/*
|--------------------------------------------------------------------------
| retJsonP (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function wraps the response in the requested callback. 
|
*/
    function retJsonP($output) {
        $callback = $_REQUEST['callback'];
        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . json_encode($output) . ');';
        } else {
            header('Content-Type: application/x-json');
            echo json_encode($output);
        }
    }

/*
|--------------------------------------------------------------------------
| retrieveList (@author : Joanna Tan)
|--------------------------------------------------------------------------
|
| This function will returns the parsed Result XML results as 
| JSON and call the function to wrap the response in the
| requested callback. 
|
| This function will call sortXML function to sort the result by 
| default to year or depending on the user selection.
| 
*/    
    public function retrieveList($condi) {
        $this->load->model('result_model');
        //part1 = setnumber, parttwo = start_record_number, part3 = sorttype, part4 = count
        list($part1, $part2, $part3, $part4) = explode("U5R", $condi);

        $sorterBaseUrl = '';
        $sorter2Url = '';
        if ($part2 == '1') {
            if ($part3 == 'na') {
                //do nothing
            } else {
                $sorterBaseUrl = $this->config->item('sortBaseUrl');
                if ($part3 == 'Year') {
                    $sorter2Url = $this->config->item('sortByYear2');
                } else if ($part3 == 'Title') {
                    $sorter2Url = $this->config->item('sortByTitle2');
                } else if ($part3 == 'Format') {
                    $sorter2Url = $this->config->item('sortByFormat2');
                } else if ($part3 == 'Author') {
                    $sorter2Url = $this->config->item('sortByAuthor2');
                } else if ($part3 == 'Callnum') {
                    $sorter2Url = $this->config->item('sortByCallnum2');
                }
                $sortXml = $sorterBaseUrl . $part1 . $sorter2Url;
                $this->result_model->sortXml($sortXml);
            }
        }

        $newnum = 0;
        $pageentriesRange = '';
        for ($i = 0; $i < $part4; $i++) {
            if (empty($pageentriesRange)) {
                $newnum = $part2;
                $pageentriesRange = $newnum;
            } else {
                $newnum = $newnum + 1;
                $pageentriesRange = $pageentriesRange . "," . $newnum;
            }
        }

        $retrieve1Url = $this->config->item('retrieve1Url');
        $retrieve2Url = $this->config->item('retrieve2Url');
        $retrieve3Url = $this->config->item('retrieve3Url');

        $xmlTwo = $retrieve1Url . $part1 . $retrieve2Url . $pageentriesRange . $retrieve3Url;
        $output = $this->result_model->praseSecondXml($xmlTwo, $part2);
        $bookdata = array();
        $bookdata['output'] = $output;
        $this->retJsonP($output);
    }
	
	public function retrieveExamList($condi) {
        $this->load->model('result_model');
        //part1 = setnumber, parttwo = start_record_number, part3 = sorttype, part4 = count
        list($part1, $part2, $part3, $part4) = explode("U5R", $condi);

        // $sorterBaseUrl = '';
        // $sorter2Url = '';
        // if ($part2 == '1') {
            // if ($part3 == 'na') {
            //    do nothing
            // } else {
                // $sorterBaseUrl = $this->config->item('sortBaseUrl');
                // if ($part3 == 'School') {
                    // $sorter2Url = $this->config->item('sortBySchool3');
                // } else if ($part3 == 'Year') {
                    // $sorter2Url = $this->config->item('sortByYear3');
                // } else if ($part3 == 'Title') {
                    // $sorter2Url = $this->config->item('sortByTitle2');
                // $sortXml = $sorterBaseUrl . $part1 . $sorter2Url;
                // $this->result_model->sortXml($sortXml);
            // }
        // }

        $newnum = 0;
        $pageentriesRange = '';
        for ($i = 0; $i < $part4; $i++) {
            if (empty($pageentriesRange)) {
                $newnum = $part2;
                $pageentriesRange = $newnum;
            } else {
                $newnum = $newnum + 1;
                $pageentriesRange = $pageentriesRange . "," . $newnum;
            }
        }

        $retrieve1Url = $this->config->item('retrieve1Url');
        $retrieve2Url = $this->config->item('retrieve2Url');
        $retrieve3Url = $this->config->item('retrieve3Url');

        $xmlTwo = $retrieve1Url . $part1 . $retrieve2Url . $pageentriesRange . $retrieve3Url;
        $output = $this->result_model->praseSecondXml($xmlTwo, $part2);
        $bookdata = array();
        $bookdata['output'] = $output;
        $this->retJsonP($output);
    }
	// }
}

?>
